const std = @import("std");
const bpf = @import("bpf");
const BPF = std.os.linux.BPF;
const helpers = BPF.kern.helpers;

const Event = extern struct {
    pid: u32,
    uid: u32,
    comm: [40]u8,
    buf: [3000]u8,
};

var events = bpf.Map.RingBuffer("events", 256, 0).init();

const tracked_func = bpf.Uprobe{ .name = "/usr/lib/x86_64-linux-gnu/libssl.so", .func = "SSL_write_ex" };

export fn uprobe_SSL_write_ex(ctx: *tracked_func.Ctx()) linksection("uprobe") callconv(.C) c_long {
    const resv = events.reserve(Event, 0);

    resv.header_ptr.*.pid = @truncate(helpers.get_current_pid_tgid());
    resv.header_ptr.*.uid = @truncate(helpers.get_current_uid_gid());
    _ = helpers.get_current_comm(&resv.header_ptr.*.comm, 40);
    _ = helpers.probe_read_user(&resv.header_ptr.*.buf, 3000, bpf.Args.cast(*[3000]u8, ctx.arg1()));

    resv.commit();

    return 0;
}
